package com.practice;

public class Practice {
    private int num = 100;
    public void add(){
        for (int i = 0; i <100000; i++) {
            synchronized (this){
                num ++;
            }
        }
    }

    public void sub(){
        for (int i = 0; i <100000 ; i++) {
            synchronized (this){
                num --;
            }
        }
    }
    public int getNum() {
        return num;
    }

    public static void main(String[] args) {
        Practice p = new Practice();
        Thread thread = new Thread(() ->{
//            synchronized (p){
                p.add();
//            }
        });
        Thread thread1 = new Thread(() ->{
//            synchronized (p){
                p.sub();
//            }

        });
        thread.start();
        thread1.start();
        try {
            thread.join();
            thread1.join();
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        System.out.println(p.getNum());
    }
}
